package axisfileserver;

import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author root
 */
public class FileServerWebService {

    
    public int CheckFile(String aTitle){
      FileLoader fileLoader = new FileLoader();
      ArrayList<File> fileList = new ArrayList();

      try{
          fileList = fileLoader.LoadFile("/tmp/Files1.xml"); //for use on the linux server
          //fileList = fileLoader.LoadFile("C:\\Files2.xml"); //for use on the windows server
      }
      catch(Exception ex){
          return -2; //file loader error
      }

      if (fileList == null) //to check if file has been loaded properly
          return -3;

      if (fileList.size()==0) //check that entities are loaded
          return -4;
      
      System.out.println("Searching for file: "+aTitle);
            for(File a:fileList){
                if(a.getName().equals(aTitle)){
                    System.out.println("File found. It needs "+a.getBandwidth()+" KB/s to download.");
                    int bandWidthAvailable = getMockBandWidth();
                    System.out.print("Checking bandwidth against requirements...");
                    if (a.getBandwidth()<bandWidthAvailable){
                       System.out.println("OK.");
                       return bandWidthAvailable;
                    }
                    else{
                        System.out.println("Not OK ... returning 0");
                        return 0;
                    }
                }
            }
            System.out.println("file not found, returning 0");
            return 0;
        }

        //makes a random bandwidth between 0-5000 unlesss doTest boolean is true
        private int getMockBandWidth(){
            Random rand = new Random();
            int temp = rand.nextInt(5000);
            System.out.println("Calculating MockBandWidth: "+temp);
            return temp;
        }
    }